﻿namespace ChinaUnicom.ViewModels
{
    using Avatar;
    using Avatar.Commands;
    using Avatar.Schedule;
    using Avatar.Themes;
    using ChinaUnicom.Controls;
    using ChinaUnicom.Models;
    using ChinaUnicom.Tasks;
    using ChinaUnicom.TrafficControl;
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;

    public class SettingsContext : PageContext, INotifyPropertyChanged
    {
        public SettingsContext()
        {
            Title = "联通助手";
            Version = "1.5";
            Copyright = "Copyright © Junhua 2012";

            authStateVisibility = Visibility.Collapsed;
            if (Authentication.Current.Authorized)
            {
                authStateVisibility = Visibility.Visible;
                Mobile = Context.Current.Account.Mobile;
            }

            TrafficLength = Traffic.Current.Length;
            Theme = ApplicationSettings.Settings.Theme;
        }

        Command logoutCommand;
        public ICommand LogoutCommand
        {
            get
            {
                if (logoutCommand == null)
                {
                    logoutCommand = new Command(param =>
                    {
                        Navigate(new Uri("/Views/SignedInfoPage.xaml", UriKind.Relative));

                        /*if (Message.Ask("确定要退出登录吗？", "温馨提示"))
                        {
                            // clear befor login.
                            Authentication.Current.Logout();
                            Context.Current.Reset();

                            GoBack();
                        }*/
                    });
                }

                return logoutCommand;
            }
        }

        Command themeSettingsCommand;
        public ICommand ThemeSettingsCommand
        {
            get
            {
                if (themeSettingsCommand == null)
                {
                    themeSettingsCommand = new Command(param =>
                    {
                        Navigate(new Uri("/Views/Themes/ThemePage.xaml", UriKind.Relative));
                    });
                }

                return themeSettingsCommand;
            }
        }

        Command trafficSettingsCommand;
        public ICommand TrafficSettingsCommand
        {
            get
            {
                if (trafficSettingsCommand == null)
                {
                    trafficSettingsCommand = new Command(param =>
                    {
                        Navigate(new Uri("/Views/Traffics/DetailsPage.xaml", UriKind.Relative));
                    });
                }

                return trafficSettingsCommand;
            }
        }

        string mobile;
        public string Mobile
        {
            get { return mobile; }
            set
            {
                if (value != mobile)
                {
                    mobile = value;
#if DEBUG
                    mobile = "18688888888";
#endif
                    NotifyPropertyChanged("Mobile");
                }
            }
        }

        Visibility authStateVisibility;
        public Visibility AuthStateVisibility
        {
            get { return authStateVisibility; }
            set
            {
                if (value != authStateVisibility)
                {
                    authStateVisibility = value;
                    NotifyPropertyChanged("AuthStateVisibility");
                }
            }
        }

        long trafficLength;
        public long TrafficLength
        {
            get { return trafficLength; }
            set
            {
                if (value != trafficLength)
                {
                    trafficLength = value;
                    NotifyPropertyChanged("TrafficLength");
                }
            }
        }

        Theme theme;
        public Theme Theme
        {
            get { return theme; }
            set
            {
                if (value != theme)
                {
                    theme = value;
                    NotifyPropertyChanged("Theme");
                }
            }
        }

        string title;
        public string Title
        {
            get { return title; }
            set
            {
                if (value != title)
                {
                    title = value;
                    NotifyPropertyChanged("Title");
                }
            }
        }

        string version;
        public string Version
        {
            get { return version; }
            set
            {
                if (value != version)
                {
                    version = value;
                    NotifyPropertyChanged("Version");
                }
            }
        }

        string copyright;
        public string Copyright
        {
            get { return copyright; }
            set
            {
                if (value != copyright)
                {
                    copyright = value;
                    NotifyPropertyChanged("Copyright");
                }
            }
        }  

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
